package cn.youframe.sample.beantest;

import cn.youframe.frame.base.BaseCtrl;
import cn.youframe.frame.beans.AjaxVo;
import cn.youframe.frame.util.SpringContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;

/**
 * User: Administrator
 * DateTime: 12-6-24 下午8:54
 * Desc:
 */
@Controller
@RequestMapping(value = "/sample")
public class BeanTestCtrl extends BaseCtrl {
    @RequestMapping(value = "/BeanTestCtrl!invokeBean.do")
    public @ResponseBody AjaxVo invokeBean(HttpServletRequest request){
        String retMsg=null;
        long startTime=System.currentTimeMillis();
        String beanID=request.getParameter("beanID");
        String methodName=request.getParameter("methodName");
        int paraNum=0;
        String para1=StringUtils.trimToEmpty(request.getParameter("para1"));
        String para2=StringUtils.trimToEmpty(request.getParameter("para2"));
        String para3=StringUtils.trimToEmpty(request.getParameter("para3"));
        String para4=StringUtils.trimToEmpty(request.getParameter("para4"));
        if(!para1.equalsIgnoreCase("")){
            paraNum++;
            if(!para2.equalsIgnoreCase("")){
                paraNum++;
                if(!para3.equalsIgnoreCase("")){
                    paraNum++;
                    if(!para4.equalsIgnoreCase(""))
                        paraNum++;
                }
            }
        }
        Class[] argsClass = null;
        Object[] args=null;
        if(paraNum>0){
            argsClass=new Class[paraNum];
            args=new Object[paraNum];
            for(int i=0;i<paraNum;i++){
                argsClass[i]=String.class;
                if(i==0){
                    args[i]=para1;
                }else if(i==1){
                    args[i]=para2;
                }else if(i==2){
                    args[i]=para3;
                }else if(i==3){
                    args[i]=para4;
                }
            }
        }
        try {
            Object boObj= SpringContext.getBean(beanID);
            Class ownerClass = boObj.getClass();
            Method method = null;
            method = ownerClass.getMethod(methodName, argsClass);
            method.invoke(boObj,args);
            long oprTime=System.currentTimeMillis()-startTime;
            retMsg="执行完成！耗时："+oprTime+" 毫秒";
        } catch (Exception e) {
            retMsg="执行失败!";
            logger.error(e.getMessage(),e);
        }
        AjaxVo ajaxVo=new AjaxVo();
        ajaxVo.setSeqId("invokeBean");
        ajaxVo.setResObj(retMsg);
        return ajaxVo;
    }
    @RequestMapping(value = "/BeanTestCtrl!page2img.do")
    public @ResponseBody AjaxVo page2img(HttpServletRequest request){
        String pageurl="http://localhost:8080/youframe/sample/beantest/BeanTest.jsp";
        String sessionId=request.getSession().getId();
        String sCurrentLine="";
        String sTotalString="";
        try{
            InputStream urlStream;
            URL url = new URL(pageurl);
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestProperty("Cookie",sessionId);
            connection.connect();
            urlStream = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(urlStream));
            while ((sCurrentLine = reader.readLine()) != null){
                sTotalString += sCurrentLine;
            }
            // 根据时间得文件名
            Calendar calendar = Calendar.getInstance();
            String fileame = String.valueOf(calendar.getTimeInMillis()) +".htm";
            fileame =System.getProperty("youframe.root")+"snap/"+fileame; //生成的html文件保存路径（snap文件夹下面）
            FileOutputStream fileoutputstream = new FileOutputStream(fileame); //建立文件输出流
            byte tag_bytes[] = sTotalString.getBytes();
            fileoutputstream.write(tag_bytes);
            fileoutputstream.close();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        AjaxVo ajaxVo=new AjaxVo();
        ajaxVo.setSeqId("invokeBean");
        ajaxVo.setResObj("网页快照！");
        return ajaxVo;
    }

    }
